package code.obj
{
	import com.darwin.darwinDll.Dll;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;

	public class IconObj
	{
		/**
		 * 凡带icon的物件都得继承这个类（建筑、英雄、士兵、装备、宝物、经验、等级......） 
		 * 
		 */
		public function IconObj()
		{
		}
		private var width:Number;
		private var height:Number
		private var limitW:Number
		private var limitH:Number
		protected var _material:DisplayObjectContainer;
		private var _containerArr:Array=[];
//		/**
//		 * 素材加载完成后会调用回调方法 
//		 */		
//		private var _callBack:Function;
		/**
		 * 得到新的图标Sprite对象，并设置宽高,并且:
		 * 没有同时设置w和h时保证同等比例缩放
		 * 否则将拉伸匹配宽高
		 * 
		 * 全都为0时保持原有宽高
		 * @param w:宽度
		 * @param h：高度
		 * @param lW,lH：限制宽高最大值在一个边长为limitW*limitH的矩形内
		 * @param callBack: 素材加载完成后会调用回调方法 
		 * @return 新的图标Sprite对象
		 * 
		 */
		public function getIcon(w:Number=0,h:Number=0,lW:Number=0,lH:Number=0,callBack:Function=null):Sprite
		{
//			_callBack = callBack;
			if(lW==0&&lH>0){
				throw new Error("不能只设置limitH而不设置limitW");
			}
			limitW = lW;
			limitH = lH||lW;
			width = w;
			height = h;
			var container:Sprite = new Sprite;
			var containerObj:ContainerObj = new ContainerObj;
			containerObj.callBack = callBack;
			containerObj.container = container;
			containerObj.iconStr = iconClassName;
			_containerArr.push(containerObj);
			if(displayObject){//如果直接取到资源了就不用再加载
				_material = displayObject as DisplayObjectContainer;
				container.addChild(_material);
				updateIconSize();
				if(Boolean(callBack)){
					callBack();
				}
			}else{					
				Dll.getInstance().getMatRes(String(serverId),_getObj,iconClassName,0,"swf",iconClassName);
			}
			return container;
		}
		/**
		 * 更新图标大小 
		 * 
		 */
		private function updateIconSize():void{
			if(!_material)return;
			if(limitW>0){
				_material.scaleX = 
					_material.scaleY = Math.min((limitW*_material.scaleX/_material.width),(limitH*_material.scaleY/_material.height));
				return;
			}
			if(width!=0&&height==0){
				_material.width = width;
				_material.scaleY=_material.scaleX;
			}else if(width==0&&height!=0){
				_material.height = height;
				_material.scaleX=_material.scaleY;
			}else if(width!=0&&height!=0){
				_material.width = width;
				_material.height = height;
			}else{
				_material.scaleX = 
					_material.scaleY = 1;
			}
		}
		private function _getObj(o:Object):void{
			if(o==null)return;
			var $class:Class = o.data as Class;
			_material = new $class() as DisplayObjectContainer;
			
			var len:int = _containerArr.length;
			for(var i:int=0;i<len;i++){
				var containerObj:ContainerObj = _containerArr[i] as ContainerObj;
				if(!containerObj.container.numChildren&&o.resKey.other==containerObj.iconStr){
					containerObj.container.addChild(_material);
					if(Boolean(containerObj.callBack)){
						containerObj.callBack();
					}
					updateIconSize();
				}
			}
			
		}
		
		private var _displayObject:DisplayObject;
		/**
		 * 直接取到现有资源，不用根据id和素材名加载
		 * @return 
		 * 
		 */
		public function get displayObject():DisplayObject
		{
			return _displayObject;
		}
		public function set displayObject(v:DisplayObject):void
		{
			_displayObject = v;
		}
		
		/**
		 *	物品显示名称 
		 */
		private var _name:String;
		public function get name():String
		{
			return _name;
		}
		public function set name(v:String):void
		{
			_name = v;
		}
		
		
		/**
		 * 物品素材serverId 
		 * @return 
		 */
		private var _serverId:String = "";
		public function get serverId():String
		{
			return _serverId;
		}
		public function set serverId(v:String):void
		{
			_serverId = v;
		}
		
		/**
		 *	图标的类名
		 */
		public function get iconClassName():String
		{
			return null;
		}
	}
}
import flash.display.Sprite;

class ContainerObj{
	public var container:Sprite;
	public var callBack:Function;
	public var iconStr:String;
}